1334B - Middle Class - CodeForces Solution


greedy sortings *1100

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    n,x=map(int,input().split())
    arr=sorted(list(map(int,input().split())))[::-1]
    h=0
    a=0
    while(h>=0 and a<n):
        if(arr[a]-x+h>=0):
            h+=arr[a]-x
            a+=1
        else:
            break
    print(a)

C++ Code:

#define ALLAH_IS_THE_GREATEST
ALLAH_IS_THE_GREATEST
#include<bits/stdc++.h>
#define ll long long
#define fl float
#define pb push_back
#include <cmath>
#define no cout << "NO\n" ;
#define yes cout << "YES\n" ;
#define elif else if
#define en <<endl;
#define ld long double
#define r0 return 0;
using namespace std;
string s,s1,h,g,o,u, h1, all = "abcdefghijklmnopqrstuvwxyz";
vector<string> sv, sw, sv1, sw1;
vector<ll> v,vv,v1, w,ww,vw ;
map<ll, ll > mp, mp1 ;
set < ll > st, st1;
float fl2, fl1;
char ch='a', ch1;
int main ()
{
    ios_base::sync_with_stdio(0);
    ll mx = -1e18,  mn = 1e18, mx1 = -10e9, mn1 = 10e9;
    ll sum=0, ans=0, q, x, y, f, e= 0,  b,t, c=0,d=0,m=0,r,k,p ;
    ll  mid=0,a, cnt = 0, n=0;
    ll l;
    cin >> t;
    for ( int i = 0 ; i < t; i ++)
    {
        cin >> a >> b ;
        for ( int j = 0 ; j < a; j ++)
        {
            cin >> n ;
            v.pb(n);
        }
        sort ( v.rbegin() , v.rend()) ;
        for (int i = 0 ; i < v.size() ; i ++ )
        {
            if (v[i] >= b)
            {
                c++ ;
                d += v[i] - b;
            }
            else
            {
               if ( d + v[i] >= b)
               {
                   c ++ ;
                   d-= b- v[i] ;
               }
            }
        }
        cout << c en
        v.clear() , d = 0 , c =0 ;
    }
}
/**

**/


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST